package com.kangyi.admin.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kangyi.admin.mapper.AdminUserMapper;
import com.kangyi.admin.mapper.NursingHomeEventMapper;
import com.kangyi.admin.service.NursingHomeEventService;
import com.kangyi.constant.MessageConstant;
import com.kangyi.context.BaseContext;
import com.kangyi.dto.AddNursingHomeEventDTO;
import com.kangyi.dto.EditNursingHomeEventDTO;
import com.kangyi.dto.NursingHomeEventPageQueryDTO;
import com.kangyi.entity.NursingHomeEvent;
import com.kangyi.exception.PermissionsException;
import com.kangyi.result.PageResult;
import com.kangyi.vo.AdminUserVO;
import com.kangyi.vo.NursingHomeEventVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blank
 * @version 1.0
 */
@Service
@Slf4j
public class NursingHomeEventServiceImpl implements NursingHomeEventService {

    @Autowired
    private NursingHomeEventMapper nursingHomeEventMapper;

    @Autowired
    private AdminUserMapper adminUserMapper;

    // 调用后检查当前线程id是否有足够的权限进行后续操作
    public void validateUserPermissions() {
        Integer currentId = BaseContext.getCurrentId();
        AdminUserVO adminUserVO = adminUserMapper.selectById(currentId);
        // 判断权限是否既不是1也不是2，如果是则抛出异常
        if (!adminUserVO.getPermissions().equals(1) && !adminUserVO.getPermissions().equals(2)) {
            throw new PermissionsException(MessageConstant.INSUFFICIENT_PRIVILEGE);
        }
    }

    @Override
    public PageResult pageQuery(NursingHomeEventPageQueryDTO nursingHomeEventPageQueryDTO) {
        validateUserPermissions();
        PageHelper.startPage(nursingHomeEventPageQueryDTO.getPage(),nursingHomeEventPageQueryDTO.getPageSize());
        Page<NursingHomeEventVO> page = nursingHomeEventMapper.pageQuery(nursingHomeEventPageQueryDTO);
        long total = page.getTotal();
        List<NursingHomeEventVO> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void addEvent(AddNursingHomeEventDTO addNursingHomeEventDTO) {
        validateUserPermissions();
        NursingHomeEvent nursingHomeEvent = new NursingHomeEvent();
        BeanUtil.copyProperties(addNursingHomeEventDTO,nursingHomeEvent);
        nursingHomeEventMapper.addEvent(nursingHomeEvent);
    }

    @Override
    public NursingHomeEventVO getEvent(Integer id) {
        validateUserPermissions();
        return nursingHomeEventMapper.getEvent(id);
    }

    @Override
    public void editEvent(EditNursingHomeEventDTO editNursingHomeEventDTO) {
        validateUserPermissions();
        NursingHomeEvent nursingHomeEvent = new NursingHomeEvent();
        BeanUtil.copyProperties(editNursingHomeEventDTO,nursingHomeEvent);
        nursingHomeEventMapper.editEvent(nursingHomeEvent);
    }

    @Override
    public void deleteEvent(List<Integer> ids) {
        validateUserPermissions();
        nursingHomeEventMapper.deleteEvent(ids);
    }

}
